package com.example.teesystem.controller;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.example.teesystem.common.utils.HttpUtil;
import com.example.teesystem.common.utils.Response;
import com.example.teesystem.common.utils.ResultCode;
import com.example.teesystem.common.utils.TimeUtil;
import com.example.teesystem.entity.Issue;
import com.example.teesystem.mapper.IssueMapper;
import com.example.teesystem.service.McffService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author pxr
 * @data 2022/6/14 9:08
 */

@RestController
@RequestMapping(value = "/tee/feature" , produces = "application/json; charset=utf-8" )
@RequiredArgsConstructor

public class FeatureController {

    private final IssueMapper issueMapper;
    private final McffService mcffService;

    //MCFF特征提取
    @PostMapping("/MCFF")
    Response FeatureMCFF(@RequestBody Map<String,String> map){
        int issueId;
        String fileName;
        try{
            issueId = Integer.parseInt(map.get("issueId"));
            fileName = map.get("fileName");
        }catch (Exception e){
            return Response.error(ResultCode.PARAMETER_ERROR);
        }
        if(fileName.equals("")){
            return Response.error(ResultCode.PARAMETER_MISS);
        }
        return mcffService.featureMCFF(issueId, fileName);
    }
    //过零率
    @PostMapping("/zeroCrossing")
    Response zeroCrossing(@RequestBody Map<String,String> request) {
        int issueId;
        String fileName;
        try{
            issueId = Integer.parseInt(request.get("issueId"));
            fileName = request.get("fileName");
        }catch (Exception e){
            return Response.error(ResultCode.PARAMETER_ERROR);
        }
        if(fileName.equals("")){
            return Response.error(ResultCode.PARAMETER_MISS);
        }
        return mcffService.zeroCrossing(issueId, fileName);
    }

    //时频图
    @PostMapping("/melSpectrogram")
    Response melSpectrogram(@RequestBody Map<String,String> request) {
        int issueId;
        String fileName;
        try{
            issueId = Integer.parseInt(request.get("issueId"));
            fileName = request.get("fileName");
        }catch (Exception e){
            return Response.error(ResultCode.PARAMETER_ERROR);
        }
        if(fileName.equals("")){
            return Response.error(ResultCode.PARAMETER_MISS);
        }
        return mcffService.melSpectrogram(issueId,fileName);
    }

    //功率谱
    @PostMapping("/power")
    Response power(@RequestBody Map<String,String> request) {
        int issueId;
        String fileName;
        try{
            issueId = Integer.parseInt(request.get("issueId"));
            fileName = request.get("fileName");
        }catch (Exception e){
            return Response.error(ResultCode.PARAMETER_ERROR);
        }
        if(fileName.equals("")){
            return Response.error(ResultCode.PARAMETER_MISS);
        }
        return mcffService.power(issueId, fileName);
    }

    //     1/3倍频程分析
    @PostMapping("/oneThree")
    Response onethree(@RequestBody Map<String,String> request) {
        int issueId;
        String fileName;
        try{
            issueId = Integer.parseInt(request.get("issueId"));
            fileName = request.get("fileName");
        }catch (Exception e){
            return Response.error(ResultCode.PARAMETER_ERROR);
        }
        if(fileName.equals("")){
            return Response.error(ResultCode.PARAMETER_MISS);
        }
        return mcffService.onethree(issueId, fileName);
    }

    //# 调制谱函数
    @PostMapping("/demonAmalysis")
    Response demon_amalysis(@RequestBody Map<String,String> request) {
        int issueId;
        String fileName;
        try{
            issueId = Integer.parseInt(request.get("issueId"));
            fileName = request.get("fileName");
        }catch (Exception e){
            return Response.error(ResultCode.PARAMETER_ERROR);
        }
        if(fileName.equals("")){
            return Response.error(ResultCode.PARAMETER_MISS);
        }
        return mcffService.demon_amalysis(issueId, fileName);
    }

    //# 调制谱函数二维
    @PostMapping("/demonAmalysis2")
    Response demon_amalysis2(@RequestBody Map<String,String> request) {
        int issueId;
        String fileName;
        try{
            issueId = Integer.parseInt(request.get("issueId"));
            fileName = request.get("fileName");
        }catch (Exception e){
            return Response.error(ResultCode.PARAMETER_ERROR);
        }
        if(fileName.equals("")){
            return Response.error(ResultCode.PARAMETER_MISS);
        }
        return mcffService.demon_amalysis2(issueId, fileName);
    }

    //低频线谱函数
    @PostMapping("/lofarV1")
    Response lofar_v1(@RequestBody Map<String,String> request) {
        int issueId;
        String fileName;
        try{
            issueId = Integer.parseInt(request.get("issueId"));
            fileName = request.get("fileName");
        }catch (Exception e){
            return Response.error(ResultCode.PARAMETER_ERROR);
        }
        if(fileName.equals("")){
            return Response.error(ResultCode.PARAMETER_MISS);
        }
        return mcffService.lofar_v1(issueId, fileName);
    }

}
